import { ref, onMounted, onUnmounted, onActivated, onDeactivated } from "vue";
import router from "@/router";
import { useOrderList } from '@/hooks/order/useOrder'
import { editOrder } from '@/api/api'
import { showConfirmDialog } from 'vant'
import { useRoute } from 'vue-router'
import { usePayStore } from "@/store/goPay";
// go Pay
export const useCartGoPay = () => {

    // title
    const onClickLeft = () => {
        history.back();
    };
    // radio
    const checked = ref(1);
    // checkbox
    const checkbox = ref(true);
    // jumpAgreePay callback
    const jumpAgreePay = () => {
        // ..
    };
    // pay_btn submit callback
    const submit = () => {
        console.log(checked.value);
        switch (checked.value) {
            case 1:
                // 调用微信支付
                // ...
                break;
            case 2:
                // 调用支付宝支付
                // ...
                break;
            case 3:
                // 调用银联支付
                // ...
                break;
            default:
                break;
        }
        router.push({ path: '/pay', query: { path: '/cart/goPay' } })
    };
    return {
        onClickLeft,
        checked,
        checkbox,
        jumpAgreePay,
        submit
    }
}

// 请求商品数据
export const useOrderContent = () => {
    const orderData = {

    }
}

// remarksOrder
export const useCartRemarks = () => {

}

// cancelOrder
export const useCancelOrder = () => {
    const payStore = usePayStore()
    // pinia接收容器
    const orderListData = ref([
        {
            goods_name: '榛果拿铁',
            goods_num: 2,
            goods_price: 27,
            goods_id: '1',
            userName: '陈奕迅',
            userPhone: 1222345688411,
            order_address: '深圳北站民治街道北站社区致远中路28号229室',
            is_sex: '男'
        },
        {
            goods_name: '生椰拿铁',
            goods_num: 1,
            goods_price: 19,
            goods_id: '2',
            userName: '陈奕迅',
            userPhone: 1222345688411,
            order_address: '深圳北站民治街道北站社区致远中路28号229室',
            is_sex: '女'
        }])
    // 最终数据
    const orderListDefalut = ref({
        uId: '',
        order_id: 1,
        goods_id: '',
        children: [
            {
                goods_name: '标准美式',
                goods_num: 1,
                goods_price: 22,
                order_remarks: '大/单份糖/单份奶/热',

            },
            {
                goods_name: '标准美式',
                goods_num: 1,
                goods_price: 22,
                order_remarks: '大/单份糖/单份奶/热',

            }
        ],
        goods_coffee_wallet: 4,
        order_time: '2022-12-17 14:22',
        order_address: '深圳宝安中心烟雨庵小区7号楼西户',
        order_delivery: 6,
        order_total_price: '',
        use_wallet: false,
        is_sex: '男',
        userName: '马龙',
        userPhone: '17860195595'
    }

    )
    onMounted(() => {
        // pinia
        // orderListData.value=payStore.orderData
        orderListDefalut.value.children = []
        orderListData.value.forEach(i => {
            orderListDefalut.value.uId = localStorage.getItem('uId');
            orderListDefalut.value.order_address = i.order_address;
            orderListDefalut.value.is_sex = i.is_sex;
            orderListDefalut.value.userName = i.userName;
            orderListDefalut.value.userPhone = i.userPhone;
            i.order_remarks = '大/单份糖/单份奶/热'
            orderListDefalut.value.children.push(i)
        });
        console.log(orderListDefalut.value);
    })
    // title
    const onClickLeft = () => {
        router.push('/order/all')
    };
    // time
    const time = ref(5 * 60 * 1000);
    // cancelOrder
    const finish_time = () => {
        // Use cancel API （id.value）
        // editOrder()
    };
    // cancel good order
    const route = useRoute();
    const orderId = ref();
    const uId = ref()
    // cancel btn callback
    const cancelOrder = () => {
        // Use cancel API （id.value）
        // ...
        showConfirmDialog({
            title: '标题',
            message:
                '您真的确认取消订单吗？',
        })
            .then(() => {
                // on confirm
                console.log(orderId.value);
                console.log(uId.value);
                // editOrder({ orderId: orderId.value, uId: orderListDefalut.value.uId, is_not: 1 }).then(res => console.log(res))
                router.push('/order/all')
            })
            .catch(() => {
                // on cancel
            });
        // jump to history page（tentative）
        //   history.back();
    };
    // gopay btn callback
    const gopay = () => {
        router.push("/cart/goPay");
    };
    
    // 页面传值
    // onMounted(() => {
    //   let pageData = '' || JSON.parse(route.query.arrData);
    //   console.log(pageData);
    //   orderId.value = pageData.orderId
    //   uId.value = pageData.uId
    //   //   时间处理
    //   let statTime = new Date(pageData.time.replace(/-/g, "/"));
    //   let nowTime = new Date();
    //   let timeD = nowTime.getTime() - statTime.getTime();
    //   //   距离订单相差的分钟数
    //   let num = parseInt(timeD / 1000);
    //   console.log(num);
    //   //判断时间是否大于5分钟(5*60)
    //   if (num > 300) {
    //     // cancel API
    //     // 返回原页面
    //     // history.back()
    //   } else {
    //     time.value = 5 * 60 * 100 - timeD;
    //   }
    // });
    // 测试组件是否销毁
    onUnmounted(() => {
        console.log('cancel组件销毁');
    })
    // 
    onActivated(() => {
        // 在首次挂载、 以及每次从缓存中重新被插入回 DOM 的时候调用
        console.log('出生');
    })

    onDeactivated(() => {
        // 在从 DOM 上移除、进入缓存 以及组件卸载时调用
        console.log('在从 DOM 上移除、进入缓存 以及组件卸载时调用');
    })
    return {
        onClickLeft,
        time,
        finish_time,
        cancelOrder,
        gopay,
        orderListData,
        orderListDefalut,
        

    }
}

// pay_complete
export const usePayComplete = () => {
    const payStore = usePayStore()
    // pinia接收容器
    const orderListData = ref([
        {
            goods_name: '榛果拿铁',
            goods_num: 2,
            goods_price: 27,
            goods_id: '1',
            userName: '陈奕迅',
            userPhone: 1222345688411,
            order_address: '深圳北站民治街道北站社区致远中路28号229室',
            is_sex: '男'
        },
        {
            goods_name: '生椰拿铁',
            goods_num: 1,
            goods_price: 19,
            goods_id: '2',
            userName: '陈奕迅',
            userPhone: 1222345688411,
            order_address: '深圳北站民治街道北站社区致远中路28号229室',
            is_sex: '女'
        }])
    // 最终数据
    const orderListDefalut = ref({
        uId: '',
        order_id: 1,
        goods_id: '',
        children: [
            {
                goods_name: '标准美式',
                goods_num: 1,
                goods_price: 22,
                order_remarks: '大/单份糖/单份奶/热',

            },
            {
                goods_name: '标准美式',
                goods_num: 1,
                goods_price: 22,
                order_remarks: '大/单份糖/单份奶/热',

            }
        ],
        goods_coffee_wallet: 4,
        order_time: '2022-12-17 14:22',
        order_address: '深圳宝安中心烟雨庵小区7号楼西户',
        order_delivery: 6,
        order_total_price: '',
        use_wallet: false,
        is_sex: '男',
        userName: '马龙',
        userPhone: '17860195595'
    }

    )
    onMounted(() => {
        // pinia
        // orderListData.value=payStore.orderData
        orderListDefalut.value.children = []
        orderListData.value.forEach(i => {
            orderListDefalut.value.uId = localStorage.getItem('uId');
            orderListDefalut.value.order_address = i.order_address;
            orderListDefalut.value.is_sex = i.is_sex;
            orderListDefalut.value.userName = i.userName;
            orderListDefalut.value.userPhone = i.userPhone;
            i.order_remarks = '大/单份糖/单份奶/热'
            orderListDefalut.value.children.push(i)
        });
        console.log(orderListDefalut.value);
    })
    onMounted(() => {
        const payStore = usePayStore()
        console.log(payStore.orderData);
    })
    // title
    const onClickLeft = () => {
        router.push('/order/all')
    };
    // cancel good order
    const route = useRoute();
    // console.log(route);
    const id = ref();
    onMounted(() => {
        // Get jump history good id
        // id.value=route.query.id
    });
    // cancel btn callback
    const cancelOrder = () => {
        // Use cancel API （id.value）
        // ...

        // jump to history page（tentative）
        history.back();
    };
    // gopay btn callback
    const gopay = () => {
        router.push("/cart/goPay");
    };
    const goMealcode = () => {
        router.push("/mealcode");
    }
    // 页面传值
    onMounted(() => {
        console.log(route.query);
    })
    return {
        onClickLeft,
        cancelOrder,
        gopay,
        orderListDefalut,
        goMealcode
    }
}